package pe.com.csi.pm.regexiv;

import java.util.List;

import pe.com.csi.pm.regexiv.bean.Parametro;
import pe.com.csi.pm.regexiv.util.UUtil;

import android.content.Intent;
import android.content.res.Configuration;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;


import android.widget.ListView;

public class PuentesPontones extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		System.out.println("OnCreate Puente Pontones");
		setContentView(R.layout.puentes_pontones);

	}

	public static class DetailsActivity extends FragmentActivity {

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
				// If the screen is now in landscape mode, we can show the
				// dialog in-line with the list so we don't need this activity.
				finish();
				return;
			}

			if (savedInstanceState == null) {
				// During initial setup, plug in the details fragment.
				DetailsFragment details = new DetailsFragment();
				details.setArguments(getIntent().getExtras());

				getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
			}
		}
	}

	public static class TitlesFragment extends ListFragment {
		boolean mDualPane;
		int mCurCheckPosition = 0;

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);

			List<Parametro> titulos = UUtil.obtenerParametrosEV(getActivity(), R.raw.puentes_items);
			setListAdapter(new ArrayAdapter<Parametro>(getActivity(), R.layout.item_menu_list, titulos));

			View detailsFrame = getActivity().findViewById(R.id.details);
			mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

			if (savedInstanceState != null) {
				mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
			}

			if (mDualPane) {
				getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
				showDetails(mCurCheckPosition);
			}
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putInt("curChoice", mCurCheckPosition);
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			showDetails(position);
		}

		/**
		 * Helper function to show the details of a selected item, either by
		 * displaying a fragment in-place in the current UI, or starting a whole
		 * new activity in which it is displayed.
		 */
		void showDetails(int index) {
			mCurCheckPosition = index;

			if (mDualPane) {
				// We can display everything in-place with fragments, so update
				// the list to highlight the selected item and show the data.
				getListView().setItemChecked(index, true);

				// Check what fragment is currently shown, replace if needed.
				DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details);
				if (details == null || details.getShownIndex() != index) {
					// Make new fragment to show this selection.
					details = DetailsFragment.newInstance(index);

					// Execute a transaction, replacing any existing fragment
					// with this one inside the frame.
					FragmentTransaction ft = getFragmentManager().beginTransaction();
					ft.replace(R.id.details, details);
					ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
					ft.commit();
				}

			} else {
				// Otherwise we need to launch a new activity to display
				// the dialog fragment with selected text.
				Intent intent = new Intent();
				intent.setClass(getActivity(), DetailsActivity.class);
				intent.putExtra("index", index);
				startActivity(intent);
			}
		}
	}

	public static class DetailsFragment extends Fragment {
		/**
		 * Create a new instance of DetailsFragment, initialized to show the
		 * text at 'index'.
		 */
		public static DetailsFragment newInstance(int index) {
			DetailsFragment f = new DetailsFragment();

			// Supply index input as an argument.
			Bundle args = new Bundle();
			args.putInt("index", index);
			f.setArguments(args);

			return f;
		}

		public int getShownIndex() {
			return getArguments().getInt("index", 0);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			if (container == null) {
				return null;
			}

			int position = getShownIndex();
			switch (position) {
			case 0:
				return inflater.inflate(R.layout.puentes_identificacion, null);
			case 1:
				return inflater.inflate(R.layout.puentes_datos_generales, null);
			case 2:
				return inflater.inflate(R.layout.puentes_tramos, null);
			case 3:
				return inflater.inflate(R.layout.puentes_tablero, null);
			case 4:
				return inflater.inflate(R.layout.puentes_subestructura, null);
			case 5:
				return inflater.inflate(R.layout.puentes_pilares, null);
			case 6:
				return inflater.inflate(R.layout.puentes_macizos, null);
			case 7:
				return inflater.inflate(R.layout.puentes_detalles, null);
			case 8:
				return inflater.inflate(R.layout.puentes_accesos, null);
			case 9:
				return inflater.inflate(R.layout.puentes_seguridad_vial, null);
			case 10:
				return inflater.inflate(R.layout.puentes_sobrecarga, null);
			case 11:
				return inflater.inflate(R.layout.puentes_ruta_alterna, null);
			case 12:
				return inflater.inflate(R.layout.puentes_suelo, null);
			case 13:
				return inflater.inflate(R.layout.puentes_aguas, null);
			case 14:
				return inflater.inflate(R.layout.puentes_puente, null);
			case 15:
				return null;
			default:
				return null;
			}

		}
	}

}
